package com.xpgk.mvc.statistic.budgetYearStatistic;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.budgetYear.Budget;
import com.xpgk.mvc.budgetYear.BudgetDetail;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.statistic.scost.ScostStatisticXLS;

public class BudgetYearStatisticController extends  BaseController{
	
	private BudgetYearStatisticService  budgetYearStatisticService;
	
	public  void  index()
	{	
		Organization  organ=Organization.dao.findById(getCUserOrganIds());
		 Calendar calendar=Calendar.getInstance();
		setAttr("organization", organ);
		setAttr("endYear", calendar.get(Calendar.YEAR)+1);
		render("budgetYearStatistic.html");
	}
	/**获取审核列表
	 * 
	 */
	public  void  getAuditinfo()
	{
		String  startYear=getPara("startYear");
		String  endYear=getPara("endYear");
		String  organIds=getPara("organIds");
		
		List<Record> result=budgetYearStatisticService.getAuditinfo(startYear,endYear,organIds);
		renderJson(result);
	}
	/**获取预算列表
	 * 
	 */
	public void getQueryinfo()
	{
		String  startYear=getPara("startYear");
		String  endYear=getPara("endYear");
		String  organIds=getPara("organIds");
		
		List<Record> result=budgetYearStatisticService.getQueryinfo(startYear,endYear,organIds);
		renderJson(result);
	}
	/**弹出店面年度预算详情
	 * 
	 */
	public void  toShowBudgetYear()
	{
		String ids=getPara("ids");
		Budget  budget=Budget.dao.findById(ids);
		Organization  organ = Organization.dao.findById(budget.getStr(Budget.column_organIds));
		setAttr("budget", budget);
		setAttr("organ", organ);
		render("budgetYearInfo.html");
	}
	/**获取门店年度预算详情
	 * 
	 */
	public  void  ShowBudgetYear()
	{
		String ids=getPara("ids");
		String organIds=getPara("organIds");
		String now=getPara("now");
		List<Record> result=budgetYearStatisticService.ShowBudgetYear(ids,organIds,now);
		renderJson(result);
	}
	
	/**审核操作
	 * 
	 */
	@Before(Tx.class)
	public  void  passverification()
	{
		String  ids=getPara("ids");
		String  checkstate=getPara("checkstate");
		String  checkReason=getPara("checkReason");
		String  msg=budgetYearStatisticService.passverification(ids,checkstate,getCUserIds(),checkReason);
		renderText(msg);
	}
	/**年度预算导出
	 * 
	 */
	public  void  excel()
	{
		String ids=getPara("ids");
		String organIds=getPara("organIds");
		String now=getPara("now");
		String oldNum=getPara("oldNum");
		String nowNum=getPara("nowNum");
		String nextNum=getPara("nextNum");
		List<Record> result=budgetYearStatisticService.ShowBudgetYear(ids,organIds,now);
		if(result!=null && result.size()>0)
		{
			for(Record record:result)
			{
				if(record.getInt("oldyearpurchased")==null)
				{
					record.set("oldyearpurchased", 0);
					record.set("oldmonthpurchased", 0);
				}
				else
				{	
					float  averageold=record.getInt("oldyearpurchased")/Integer.parseInt(oldNum);
					record.set("oldmonthpurchased",String.format("%.2f", averageold));
				}
				if(record.getBigDecimal("now9purchased")==null){record.set("now9purchased",new BigDecimal(0));}
				if(record.getInt("now10willpurchased")==null){record.set("now10willpurchased", 0);}
				if(record.getInt("now11willpurchased")==null){record.set("now11willpurchased", 0);}
				if(record.getInt("now12willpurchased")==null){record.set("now12willpurchased", 0);}
				record.set("nowyearwillpurchased",Integer.parseInt(record.getBigDecimal("now9purchased").toString())+record.getInt("now10willpurchased")+record.getInt("now11willpurchased")+record.getInt("now12willpurchased"));
				float  averagenow=record.getInt("nowyearwillpurchased")/Integer.parseInt(nowNum);
				record.set("nowmonthwillpurchased",String.format("%.2f", averagenow));
				record.set("yearsum",record.getInt("januarynum")+record.getInt("februarynum")+record.getInt("marchnum")+record.getInt("aprilnum")+record.getInt("maynum")+
						record.getInt("junenum")+record.getInt("julynum")+record.getInt("augustnum")+record.getInt("septembernum")+record.getInt("octobernum")+
						record.getInt("novembernum")+record.getInt("decembernum"));
				float  averagenext=record.getInt("yearsum")/Integer.parseInt(nextNum);
				record.set("monthnum",String.format("%.2f", averagenext));
			}
		}
		
		
		
		//搜索条件
		Organization  organ  =  Organization.dao.findById(organIds);
		String search="门店："+organ.getStr(Organization.column_name)+","+(Integer.parseInt(now)-1)+"营业时间："+oldNum+
				","+Integer.parseInt(now)+"营业时间："+nowNum+","+(Integer.parseInt(now)+1)+"营业时间："+nextNum;
		
		render(new BudgetYearStatisticXLS(result,search,"年度预算查询表"));
	}
}
